import {
	Column,
	Entity,
	JoinTable,
	ManyToMany,
	OneToMany,
	PrimaryGeneratedColumn
} from "typeorm";
import { Exclude } from "class-transformer";
import { Role } from "@/modules/role/entities/role.entity";
import { EntityBase } from "@/common/entities/entity.base";
import { GenderType, StatusValue } from "@/enum/common.enum";
import { ApiProperty } from "@nestjs/swagger";
import { $enum } from "ts-enum-util";
import { UserRole } from "@/modules/user-role/entities/user-role.entity";


@Entity()
export class User extends EntityBase {
	@PrimaryGeneratedColumn()
	id: number;
	
	@ApiProperty({ type: String, description: "用户名" })
	@Column({ type: "varchar", nullable: false, unique: true, comment: "用户名" })
	username: string;
	
	@ApiProperty({ type: String, description: "用户登录密码" })
	@Column({ type: "varchar", nullable: false, comment: "用户登录密码" })
	@Exclude({ toPlainOnly: true }) // 输出屏蔽盐
	password: string;
	
	@ApiProperty({ type: String, enum: $enum(GenderType).getValues(), description: "性别：1-男 2-女" })
	@Column({ type: "tinyint", default: GenderType.WOMAN, comment: "性别：1-男 2-女" })
	gender: number;
	
	@ApiProperty({ type: String, description: "用户手机号码" })
	@Column({ type: "varchar", length: 20, default: "", comment: "用户手机号码" })
	photo: string;
	
	@ApiProperty({ type: String, description: "用户地址" })
	@Column({ type: "varchar", default: "", comment: "用户地址" })
	address: string;
	
	@ApiProperty({ type: Number, description: "账户余额" })
	@Column({ type: "decimal", default: 0, comment: "账户余额" })
	balance: number;
	
	@ApiProperty({ type: String, description: "头像url" })
	@Column({ type: "varchar", default: "", comment: "头像地址" })
	public avatar: string;
	
	@ApiProperty({ type: String, enum: $enum(StatusValue).getValues(), description: "状态：0-禁用 1-启用" })
	@Column({ type: "tinyint", default: StatusValue.NORMAL, comment: "状态：0-禁用 1-启用" })
	status: number;
	
	// @ManyToMany(() => Role, (roles) => roles.users)
	// @JoinTable({
	// 	// name: "user_role", // 指定中间表的名称
	// 	// joinColumn: { name: "userId" }, // 指定User实体类在中间表中的列名
	// 	// inverseJoinColumn: { name: "roleId" } // 指定Role实体类在中间表中的列名
	// })
	// roles: Role[];
	@OneToMany(() => UserRole, (userRole) => userRole.user)
	userRole: UserRole[];
	// @OneToMany(() => Logs, (logs) => logs.user, { cascade: true })
	// logs: Logs[];
	
}
